﻿Public Class frmConectaSerial

    Private Sub frmConectaSerial_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        CargaPuertos()
        CheckForIllegalCrossThreadCalls = False

    End Sub

    Sub ConectaPuerto()
        Try
            With commUSB
                If .IsOpen Then
                    .Close()
                End If

                .PortName = lstPuerto.Text
                .BaudRate = 57600
                .DataBits = 8
                .StopBits = IO.Ports.StopBits.One
                .Parity = IO.Ports.Parity.None
                .DtrEnable = False
                .Handshake = IO.Ports.Handshake.None
                .ReadBufferSize = 4096
                .WriteBufferSize = 2048
                .ReceivedBytesThreshold = 1
                .WriteTimeout = -1
                .Encoding = System.Text.Encoding.Default
                .Open()

                frmModuloDiesel.txtLitros.Enabled = False
                frmModuloDiesel.Button1.Enabled = True

            End With
        Catch ex As Exception
            frmModuloDiesel.txtLitros.Enabled = True
            frmModuloDiesel.Button1.Enabled = False
            MsgBox("No se pudo conectar al dispensario", MsgBoxStyle.Critical, "Aviso")
        End Try
    End Sub
    Sub CargaPuertos()
        Dim l As Integer
        Dim nCom As String

        Try
            lstPuerto.Items.Clear()
            For Each sp As String In My.Computer.Ports.SerialPortNames
                l = sp.Length
                If ((sp(l - 1) >= "0") And (sp(l - 1) <= "9")) Then
                    lstPuerto.Items.Add(sp)
                Else
                    nCom = sp.Substring(0, l - 1)
                    lstPuerto.Items.Add(nCom)
                End If
            Next
            If lstPuerto.Items.Count >= 1 Then
                lstPuerto.Text = lstPuerto.Items(0)
            Else
                lstPuerto.Text = ""
            End If


        Catch ex As Exception

        End Try
    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        ConectaPuerto()

    End Sub
End Class